// ANES24PilotEconomics
log using ANES24PilotEconomics.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\anes_pilot_2024_dta_20240319.dta" // American National Election Study 2024 Pilot Study, March 19, 2024 Version. Date accessed: March 09, 2025.

keep if sample_type == 1 // Only keep those respondents with proper weight, as advised in codebook

//Create social justice scale
*Delete missing values
replace group_antifa= . if inlist(group_antifa, -7, 999)

*Generate a dummy variable for raceadvantage
gen raceadwhite=. 
replace raceadwhite=1 if raceadvt_white==3 & raceadvt_whitestr==1
replace raceadwhite=2 if raceadvt_white==3 & raceadvt_whitestr==2
replace raceadwhite=3 if raceadvt_white==3 & raceadvt_whitestr==3
replace raceadwhite=4 if raceadvt_white==2
replace raceadwhite=5 if raceadvt_white==1 & raceadvt_whitestr==3
replace raceadwhite=6 if raceadvt_white==1 & raceadvt_whitestr==2
replace raceadwhite=7 if raceadvt_white==1 & raceadvt_whitestr==1

*Reverse code certain items so social justice values are high
foreach var in police_number trans_health school_gender {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rschool_gender rtrans_health rpolice_number raceadwhite group_blm group_antifa {
    summarize `var'
    gen s_`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in s_rschool_gender s_rtrans_health s_rpolice_number s_raceadwhite s_group_blm s_group_antifa {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_score = 0
gen count_nonzero = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in s_rschool_gender s_rtrans_health s_rpolice_number s_raceadwhite s_group_blm s_group_antifa {
    replace total_score = total_score + `var'
    replace count_nonzero = count_nonzero + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = . 
replace SocJusValues = total_score / count_nonzero if count_nonzero > 0

// Economics
*Delete missing values
replace faminc_new = . if inlist(faminc_new, 97, -7) 
replace stloan_amtowe=. if stloan_amtowe<0 
replace empsat_satisf=. if empsat_satisf<0 
replace emploss_losejob=. if emploss_losejob<0 
replace emploss_less=. if emploss_less<0 
replace emploss_ai=. if emploss_ai<0 
replace emploss_robot=. if emploss_robot<0 
replace secure_ease=. if secure_ease<0 
replace secure_parents=. if secure_parents<0 
replace empsuperv_superv=. if empsuperv_superv<0 
replace empsuperv_selfsuperv=. if empsuperv_selfsuperv<0 

*Create unemployment dummy
gen unemployed=. 
replace unemployed=0 if inlist(employ, 1, 2)
replace unemployed=1 if inlist(employ, 3, 4)

*Reverse code so economic precarity is low
foreach var in stloan_amtowe empsat_satisf emploss_losejob emploss_less emploss_ai emploss_robot secure_ease secure_parents infl_behav_5 unemployed empsuperv_superv empsuperv_selfsuperv {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

pwcorr SocJusValues rstloan_amtowe rempsat_satisf remploss_losejob remploss_less remploss_ai remploss_robot rsecure_ease rsecure_parents infl_behav_1 infl_behav_2 infl_behav_3 infl_behav_4 rinfl_behav_5 runemployed rempsuperv_superv rempsuperv_selfsuperv faminc_new [aweight=weight], sig 

log close


